Home:ALL Converter>Private inheritance hides base class with "inaccessible within this context" error

Private inheritance hides base class with "inaccessible within this context" error

Ask Time:2011-08-27T01:18:00         Author:stephen_liu

Json Formatter

I'm having a problem similar to that described in Private inheritance renders class inaccessible where a privately inherited base class gives an "inaccessible within this context" error when I try to declare a member of the base class inside the derived class.

Explicitly referencing X with ::X works in the above case, but what if the code is in a function such as:

void fooby()
{
    class X {};

    class Y : private X {};

    class Z : public Y
    {
    public:
        X x; // Compiler "inaccessible within this context" error
    };
};

How do you reference X in this case?

If fooby were a struct/class, then ::fooby::X would work, but I'm not sure how to do it in the case above.

Author:stephen_liu,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7208309/private-inheritance-hides-base-class-with-inaccessible-within-this-context-err
David Rodríguez - dribeas :

The problem that you are facing is that there is an injected identifier X in Y (and all derived types) that refers to X, which is not accessible below Y.\nIn the common case of user-defined types that are declared at namespace level, you could use the namespace to qualify the type and gain access:\nclass X {};\nclass Y : X {};\nclass Z : Y {\n ::X x; // Or Namespace::X\n};\n\nBecause you are defining your types inside a function that is not a valid option.\nAlternatively, you can get around the problem with other workarounds. As hamstergene proposed, you can create an alternative identifier to refer to X:\ntypedef class X {} another_name;\nclass Y : X {};\nclass Z : Y {\n another_name x;\n};\n\nOr you can add a typedef inside Y to provide the type to derived types:\nclass X {};\nclass Y : X {\npublic:\n typedef X X;\n};\nclass Z : Y {\n X x;\n};\n\nThis last option works, because it will add an X identifier inside Y that is public and refers to the type, so the compiler will find the type there and use that in Z.",
2011-08-26T18:19:00
yy